package com.iv.durian.cloud.api;

import com.iv.durian.cloud.JsonResult;
import com.iv.durian.cloud.Student;
import com.iv.durian.cloud.api.service.StudentService;
import com.iv.durian.cloud.vo.StudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/student")
@Slf4j
@Api(value = "StudentApi" ,description = "studentApi",tags={"student"} )
public class StudentApi {

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "获取学员信息")
    @RequestMapping(value = "/StudentApi", method = RequestMethod.GET)
    public ResponseEntity<JsonResult<StudentVO>> getStudent (
            @RequestParam(value = "studentId") Integer id){
        JsonResult r = new JsonResult();
        try {
            Student student = studentService.getStudentById(id);
            if (student !=null) {
                StudentVO studentVO = StudentVO.builder()
                        .id(student.getId())
                        .userId(student.getUserId())
                        .name(student.getName())
                        .birthday(student.getBirthday())
                        .createTime(student.getCreateTime())
                        .updateTime(student.getUpdateTime()).build();
                r.ok(studentVO);
            }
        }catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("学员信息查询失败",e);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "添加学员信息")
    @RequestMapping(value = "/student/add", method = RequestMethod.POST)
    public ResponseEntity<JsonResult<StudentVO>> addStudent (
            @ApiParam(name = "name",value = "姓名",required = true)
            @RequestParam(value = "name") String name,
            @ApiParam(name = "birthday",value = "生日",required = true)
            @RequestParam(value = "birthday") Date birthday,
            @ApiParam(name = "userId",value = "userId",required = true)
            @RequestParam(value = "userId") Integer userId
            ){
        JsonResult r = new JsonResult();
        try {
            Student student = new Student();
            student.setName(name);
            student.setBirthday(birthday);
            student.setUserId(userId);
            studentService.addStudent(student);
        }catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("学员信息添加失败",e);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "修改学员信息")
    @RequestMapping(value = "/student/update", method = RequestMethod.POST)
    public ResponseEntity<JsonResult<StudentVO>> updateStudent (
            @ApiParam(name = "id",value = "学员ID",required = true)
            @RequestParam(value = "id") Integer id,
            @ApiParam(name = "name",value = "姓名",required = true)
            @RequestParam(value = "name") String name,
            @ApiParam(name = "birthday",value = "生日",required = true)
            @RequestParam(value = "birthday") Date birthday,
            @ApiParam(name = "userId",value = "userId",required = true)
            @RequestParam(value = "userId") Integer userId
    ){
        JsonResult r = new JsonResult();
        try {
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setBirthday(birthday);
            student.setUserId(userId);
            studentService.update(student);
        }catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("学员信息修改失败",e);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "删除学员")
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    private void remove(
            @ApiParam(required = true, name = "id", value = "学员id")
            @PathVariable("id") Integer id
    ){
        log.debug("打印删除的学员id:" + id);
        studentService.remove(id);
    }
}
